Shader "UI/GuideMask"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        
        // 挖洞区域参数
        _HoleCenter ("Hole Center", Vector) = (0.5, 0.5, 0, 0)
        _HoleSize ("Hole Size", Vector) = (0.1, 0.1, 0, 0)
        _HoleShape ("Hole Shape (0=Rectangle, 1=Circle, 2=RoundedRect)", Float) = 1
        _CornerRadius ("Corner Radius", Float) = 0.05
        _Softness ("Edge Softness", Float) = 0.01
        
        // 动画参数
        _AnimProgress ("Animation Progress", Range(0, 1)) = 1
        _AnimType ("Animation Type (0=Scale, 1=Fade)", Float) = 0
        
        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255
        
        _ColorMask ("Color Mask", Float) = 15
        
        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    }

    SubShader
    {
        Tags
        { 
            "Queue"="Transparent" 
            "IgnoreProjector"="True" 
            "RenderType"="Transparent" 
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        
        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp] 
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

        Pass
        {
            Name "Default"
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            #pragma multi_compile __ UNITY_UI_ALPHACLIP
            
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord  : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };
            
            // 声明所有需要的变量
            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            float4 _ClipRect;

            // 挖洞区域参数
            float4 _HoleCenter;
            float4 _HoleSize;
            float _HoleShape;
            float _CornerRadius;
            float _Softness;
            
            // 动画参数
            float _AnimProgress;
            float _AnimType;

            v2f vert(appdata_t IN)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(IN);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.worldPosition = IN.vertex;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                
                OUT.color = IN.color * _Color;
                return OUT;
            }

            // 计算到矩形中心的距离
            float rectDistance(float2 p, float2 center, float2 size)
            {
                float2 d = abs(p - center) - size * 0.5;
                return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
            }

            // 计算到圆心的距离
            float circleDistance(float2 p, float2 center, float radius)
            {
                return length(p - center) - radius;
            }

            // 计算到圆角矩形的距离
            float roundedRectDistance(float2 p, float2 center, float2 size, float radius)
            {
                float2 d = abs(p - center) - size * 0.5 + radius;
                return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - radius;
            }

            // 自定义UI裁剪函数，替代UnityGetUI2DClipping
            float UnityGetUI2DClippingCustom(float2 position, float4 clipRect)
            {
                // 如果裁剪区域无效，返回1（不裁剪）
                if (clipRect.z <= clipRect.x || clipRect.w <= clipRect.y)
                    return 1.0;
                
                // 计算点是否在裁剪矩形内
                float2 inside = step(clipRect.xy, position) * step(position, clipRect.zw);
                return inside.x * inside.y;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
                
                // 使用自定义的UI裁剪函数替代UnityGetUI2DClipping
                color.a *= UnityGetUI2DClippingCustom(IN.worldPosition.xy, _ClipRect);
                
                // 计算当前点到挖洞中心的距离
                float2 uv = IN.texcoord;
                float dist = 0;
                
                if (_HoleShape < 0.5) // 矩形
                {
                    dist = rectDistance(uv, _HoleCenter.xy, _HoleSize.xy);
                }
                else if (_HoleShape < 1.5) // 圆形
                {
                    float radius = max(_HoleSize.x, _HoleSize.y) * 0.5;
                    dist = circleDistance(uv, _HoleCenter.xy, radius);
                }
                else // 圆角矩形
                {
                    dist = roundedRectDistance(uv, _HoleCenter.xy, _HoleSize.xy, _CornerRadius);
                }
                
                // 应用动画效果
                float animatedDist = dist;
                if (_AnimType < 0.5) // 缩放动画
                {
                    animatedDist = lerp(max(_HoleSize.x, _HoleSize.y), dist, _AnimProgress);
                }
                else // 透明度动画
                {
                    animatedDist = dist;
                }
                
                // 计算透明度 - 使用内置的smoothstep函数
                float alpha = smoothstep(-_Softness, _Softness, animatedDist);
                
                // 应用透明度动画
                if (_AnimType >= 0.5) // 透明度动画
                {
                    alpha = lerp(1, alpha, _AnimProgress);
                }
                
                color.a *= alpha;
                
                #ifdef UNITY_UI_ALPHACLIP
                clip(color.a - 0.001);
                #endif

                return color;
            }
        ENDCG
        }
    }
}